package program.atm.data;

import javax.swing.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 银行卡操作类.
 *
 * @author 林志鹏
 * @version 1.0.0
 * @date 2020/11/8 14:26
 */
public class CustomerAccount extends Card {

    public CustomerAccount(String number, String password, int money) {
        super(number, password, money);
    }

    /**
     * 存款.
     * 要求存钱为100的整数倍
     *
     * @param money
     * @return
     */
    public boolean addMoney(int money) {
        if (money % 100 != 0) {
            JOptionPane.showMessageDialog(null, "存款失败！存款数目需为100得整数倍", "提示消息", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        //余额
        int countMoney = getMoney() + money;
        setMoney(countMoney);
        //日期格式化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //记录交易记录
        List<String> operationRecod = getOperationRecod();
        operationRecod.add("【" + LocalDateTime.now().format(dateTimeFormatter) + "】 ATM 存款 [" + money + "]元，当前余额[" + countMoney + "]元");
        JOptionPane.showMessageDialog(null, "存款成功！", "提示消息", JOptionPane.WARNING_MESSAGE);
        return true;
    }

    /**
     * 取款.
     * 每次取款金额为100的倍数，
     * 总额不超过5000元，
     * 支取金额不允许透支
     */
    public boolean lessMoney(int money) {
        if (money % 100 != 0) {
            JOptionPane.showMessageDialog(null, "取款失败！取款数目需为100得整数倍", "提示消息", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        if (money > 5000) {
            JOptionPane.showMessageDialog(null, "单次取款总额不得大于5000", "提示消息", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        int currentMoney = getMoney();
        if (currentMoney < money) {
            JOptionPane.showMessageDialog(null, "当前余额 【" + currentMoney + "】 小于取款金额【" + money + "】，不允许透支", "提示消息", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        //取款
        int result = currentMoney - money;
        setMoney(result);
        //日期格式化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //记录交易记录
        List<String> operationRecod = getOperationRecod();
        operationRecod.add("【" + LocalDateTime.now().format(dateTimeFormatter) + "】 ATM 取款 [" + money + "]元，当前余额[" + result + "]元");
        JOptionPane.showMessageDialog(null, "取款成功！", "提示消息", JOptionPane.WARNING_MESSAGE);
        return true;
    }

    /**
     * 导出交易记录到文件.
     */
    public void exportRecord() {
        //获取记录
        List<String> operationRecod = getOperationRecod();
        if (operationRecod.isEmpty()) {
            JOptionPane.showMessageDialog(null, "当前账号交易记录为空！", "提示消息", JOptionPane.WARNING_MESSAGE);
            return;
        }
        //获取当前jar所在路径
        String jarPath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        //避免中文情况，转码
        try {
            jarPath = URLDecoder.decode(jarPath, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //拼接输出的目录
        String path = jarPath.substring(0, jarPath.lastIndexOf("/")) + "/AccountRecord.txt";
        File file = new File(path);
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            for (String record : operationRecod) {
                out.write(record + "\r\n");
            }
            out.flush();
            out.close();
            JOptionPane.showMessageDialog(null, "文件成功导出至【"+path+"】目录下");
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "文件导出异常", "错误消息", JOptionPane.WARNING_MESSAGE);
            e.printStackTrace();
        }
    }

    /**
     * 设置新密码.
     *
     * @param newPwd 新密码
     * @param oldPwd 旧密码
     * @param check  确认
     */
    public boolean updatePassword(String oldPwd, String newPwd, String check) {
        if (!getPassword().equals(oldPwd)) {
            JOptionPane.showMessageDialog(null, "输入旧密码错误！", "提示消息", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        if (newPwd.length() < 6 || isAllSame(newPwd)) {
            JOptionPane.showMessageDialog(null, "密码不符合要求！", "提示消息", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        if (!newPwd.equals(check)) {
            JOptionPane.showMessageDialog(null, "两次输入密码不一致！", "提示消息", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        setPassword(newPwd);
        JOptionPane.showMessageDialog(null, "设置新密码成功！", "提示消息", JOptionPane.WARNING_MESSAGE);
        return true;
    }

    /**
     * 判断一个字符串是否由相同字符组成.
     * 新密码不能6位全相同，所以判断
     * 如666666不则允许
     * 666665允许
     *
     * @return
     */
    private boolean isAllSame(String str) {
        char[] chars = str.toCharArray();
        int i;
        for (i = 0; i < chars.length; i++) {
            if (chars[0] != chars[i]) {
                break;
            }
        }
        if (i >= chars.length) {
            return true;
        }
        return false;
    }

}
